package com.fishery.processing.dao;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fishery.processing.entity.Factory;
import com.fishery.processing.entity.Input;
import com.fishery.processing.entity.vo.FactoryMessageVo;
import com.fishery.processing.entity.vo.FactoryOfBaseVo;
import com.fishery.processing.mapper.FactoryMapper;
import com.fishery.processing.mapper.InputMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.xml.ws.FaultAction;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * @author run
 * @since 2021/1/14 16:41
 */
@Component
public class FactoryDao {

    @Autowired
    private FactoryMapper factoryMapper;

    public int addFactory(Factory factory){
        return factoryMapper.insert(factory);
    }

    public int removeFactory(String factoryId){
        return factoryMapper.deleteById(factoryId);
    }

    public int updateFactory(Factory factory){
        return factoryMapper.updateById(factory);
    }

    public Factory findFactoryById(String factoryId){
        return factoryMapper.selectById(factoryId);
    }

    public List<Factory> findFactoryByBaseAndName(Factory factory){
        Map<String,Object> map = new HashMap<>(2,1);
        map.put("base_id",factory.getBaseId());
        map.put("processing_factory_name",factory.getProcessingFactoryName());
        return factoryMapper.selectByMap(map);
    }

    public IPage<Factory> findFactoriesOfBase(String baseId,Integer page,Integer size){
        Page<Factory> factoryPage = new Page<>(page,size);
        QueryWrapper<Factory> wrapper = new QueryWrapper<>();
        wrapper.eq("base_id",baseId);
        return factoryMapper.selectPage(factoryPage,wrapper);
    }

    public List<Factory> findFactoriesOfBase(String baseId){
        Map<String,Object> map = new HashMap<>(1,1);
        map.put("base_id",baseId);
        return factoryMapper.selectByMap(map);
    }

    public List<Factory> findFactoryOfType(String type){
        HashMap<String,Object> map = new HashMap<>(1,1);
        map.put("processing_type",type);
        return factoryMapper.selectByMap(map);
    }

    public IPage<Factory> findFactoryOfType(String type,Integer page,Integer size){
        QueryWrapper<Factory> wrapper = new QueryWrapper<>();
        wrapper.eq("processing_type",type);
        Page<Factory> factoryPage = new Page<>(page,size);
        return factoryMapper.selectPage(factoryPage,wrapper);
    }

    public FactoryOfBaseVo findFactoryMessageOfBase(String baseId){
        return factoryMapper.findFactoryMessageOfBase(baseId);
    }

    public List<FactoryMessageVo> findFactoryMessageOfAll(){
        return factoryMapper.findFactoryMessageOfAll();
    }
}
